package com.pitb.citizenreporting;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.pitb.citizenreporting.db.HakathonDb;

public class MyComplaints extends Activity{
	ListView lv;
	TextView tv;
	ComplaintAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_issues);
		lv = (ListView) findViewById(R.id.list);
		tv = (TextView) findViewById(R.id.emptytext);
		HakathonDb db = new HakathonDb();
		List<ReportModel> model = db.getEntries();
		adapter = new ComplaintAdapter(this,model);
		lv.setAdapter(adapter);
		if(model.size() == 0){
			tv.setVisibility(View.VISIBLE);
		}
		lv.setItemsCanFocus(false);
		lv.setFocusable(false);
		
		lv.setDividerHeight(1);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				ReportModel model = adapter.getItem(position);
				Intent intent = new Intent(MyComplaints.this,ComplaintDetails.class);
				intent.putExtra(ComplaintDetails.TRACK_ID, model.traking_id);
				intent.putExtra(ComplaintDetails.TRACK_PROBLEM, model.problem);
				intent.putExtra(ComplaintDetails.TRACK_STATUS, model.status);
				startActivity(intent);
			}
		});
	}
	
	class ComplaintAdapter extends BaseAdapter {
		List<ReportModel> list;
		private LayoutInflater mInflater;
		public int selectedpos = -1;
		
		public ComplaintAdapter(Context arg,List<ReportModel> arg1) {
			mInflater = (LayoutInflater) arg
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			list = arg1 ;			
		}

		public void addFiles(List<ReportModel> arg) {
			list.addAll(arg);
		}

		public void addFile(ReportModel arg) {
			if (!list.contains(arg)) {
				list.add(arg);
			}
		}

		public void clearAll() {
			list.clear();
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public ReportModel getItem(int arg0) {
			return list.get(arg0);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			if (position >= list.size()) {
				return null;
			}			
			ViewHolder holder;
			if (convertView == null) {
				if (mInflater == null) {
					return null;
				}
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.listitems, null);
				holder.id = (TextView) convertView.findViewById(R.id.item1);
				holder.id.setTextColor(Color.WHITE);
				holder.problem = (TextView) convertView.findViewById(R.id.item2);
				holder.problem.setTextColor(Color.WHITE);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			ReportModel model = getItem(position);
			holder.problem.setText(model.problem);
			holder.id.setText(model.traking_id);

			
			return convertView;
		}		
	}

	static class ViewHolder {
		TextView id;		
		TextView problem ;
	}
}
